package com.lsp.server.product.controller;

import com.lsp.server.common.Apiresponse;
import com.lsp.server.product.domain.po.ProductShop;
import com.lsp.server.product.service.ShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName ShopController
 * @Description 商品管理控制层
 * @Author Mr Hao
 * @Date 2021/11/25 14:36
 */
@RestController
@RequestMapping("/shop")
public class ShopController {

    @Autowired
    private ShopService shopService;

    /**
     *
     * @return
     */
    @PostMapping("/getChildren")
    public Apiresponse getChildren(){
        return Apiresponse.success(shopService.queryAllDataStr());
    }

    /**
     *
     * @param tid
     * @return
     */
    @PostMapping("/queryAttrValues")
    public Apiresponse getAttrValues(Integer tid){
        try {
            if (null == tid){
                return Apiresponse.Error(400,"参数错误");
            }
            return Apiresponse.success(shopService.getAttrValues(tid));
        }catch (Exception e){
            return Apiresponse.Error(500,e.getMessage());
        }
    }

    /**
     * 新增数据
     * @return
     */
    @PostMapping("/addShop")
    public Apiresponse AddShop(ProductShop shop,String valuess,String imgs){
        try {
            if (shop.getName() != null)
            shopService.addShop(shop,valuess,imgs);
            return Apiresponse.success("");
        }catch (Exception e){
            return Apiresponse.Error(500,e.getMessage());
        }
    }

    /**
     * 获取商品数据
     * @param page
     * @param size
     * @param name
     * @return
     */
    @PostMapping("/list")
    public Apiresponse queryList(Integer page,Integer size,String name){

            Map map = new HashMap();
            map.put("page",page);
            map.put("size",size);
            map.put("name",name);
            return Apiresponse.success(shopService.queryList(map));

    }

    /**
     * 查询附图数据
     * @return
     */
    @PostMapping("/imgList")
    public Apiresponse imgList(Integer id){
       try {
           if (id == null){
               return Apiresponse.Error(400,"参数异常");
           }
           return Apiresponse.success(shopService.imgList(id));
       }catch (Exception e){
           return Apiresponse.Error(500,e.getMessage());
       }
    }

    /**
     * 查询回显数据
     */
    @PostMapping("/queyrValuesByPid")
    public Apiresponse queyrValuesByPid(Integer pid){
        return Apiresponse.success(shopService.queryShopValuesByPid(pid));
    }
    @RequestMapping("updateValuesByPid")
    public Apiresponse updateValuesByPid(Integer pid,String values){
        shopService.updateValuesByPid(pid,values);
        return Apiresponse.success("");
    }


}
